<template>
  <div class="login-container">
    <el-form
      ref="registerFormRef"
      :model="registerData"
      :rules="registerRules"
      class="login-form"
    >
      <div class="flex text-white items-center py-4">
        <span class="text-2xl flex-1 text-center">testnest-register</span>
        <lang-select class="text-white! cursor-pointer" />
      </div>

      <el-form-item prop="name">
        <div class="p-2 text-white">
          <svg-icon icon-class="user" />
        </div>
        <el-input
          v-model="registerData.name"
          class="flex-1"
          size="large"
          placeholder="用户名"
        />
      </el-form-item>

      <el-tooltip
        :disabled="isCapslock === false"
        content="Caps lock is On"
        placement="right"
      >
        <el-form-item prop="password">
          <span class="p-2 text-white">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            v-model="registerData.password"
            class="flex-1"
            placeholder="密码"
            :type="passwordVisible === false ? 'password' : 'input'"
            size="large"
            @keyup="checkCapslock"
          />
          <span class="mr-2" @click="passwordVisible = !passwordVisible">
            <svg-icon
              :icon-class="passwordVisible === false ? 'eye' : 'eye-open'"
              class="text-white cursor-pointer"
            />
          </span>
        </el-form-item>
      </el-tooltip>

      <el-tooltip
        :disabled="isCapslock === false"
        content="Caps lock is On"
        placement="right"
      >
        <el-form-item prop="confirmPassword">
          <span class="p-2 text-white">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            v-model="registerData.confirmPassword"
            class="flex-1"
            placeholder="确认密码"
            :type="passwordVisible === false ? 'password' : 'input'"
            size="large"
            @keyup="checkCapslock"
          />
          <span class="mr-2" @click="passwordVisible = !passwordVisible">
            <svg-icon
              :icon-class="passwordVisible === false ? 'eye' : 'eye-open'"
              class="text-white cursor-pointer"
            />
          </span>
        </el-form-item>
      </el-tooltip>

      <el-form-item prop="email">
        <span class="p-2 text-white">
          <svg-icon icon-class="email" />
        </span>
        <el-input
          v-model="registerData.email"
          class="flex-1"
          placeholder="邮箱"
          size="large"
        />
      </el-form-item>


      <el-form-item prop="desc">
        <span class="p-2 text-white">
          <svg-icon icon-class="description" />
        </span>
        <el-input
          v-model="registerData.desc"
          class="flex-1"
          placeholder="描述"
          size="large"
        />
      </el-form-item>


      <el-button
        size="default"
        :loading="loading"
        type="primary"
        class="w-full"
        @click.prevent="handleLogin"
        >注册
      </el-button>


    </el-form>
  </div>
</template>

<script setup lang="ts">
import router from "@/router";
import LangSelect from "@/components/LangSelect/index.vue";
import SvgIcon from "@/components/SvgIcon/index.vue";
import { ElMessage } from 'element-plus'

// 状态管理依赖
import { useUserStore } from "@/store/modules/user";

// API依赖
import { LocationQuery, LocationQueryValue, useRoute } from "vue-router";
import { getCaptchaApi, loginTest, registerTest} from "./server";
import { RegisterData } from "@/api/register/types";

const userStore = useUserStore();
const route = useRoute();

/**
 * 按钮loading
 */
const loading = ref(false);
/**
 * 是否大写锁定
 */
const isCapslock = ref(false);
/**
 * 密码是否可见
 */
const passwordVisible = ref(false);
/**
 * 验证码图片Base64字符串
 */
 const captchaBase64Data = ref();

/**
 * 注册表单引用
 */
const registerFormRef = ref(ElForm);

const registerData = ref<RegisterData>({
  name:'',
  password: '',
  confirmPassword: '',
  desc: '',
  email: ''
});

const registerRules = {
  name: [{ required: true, trigger: "blur","message": "请输入用户名"}],
  password: [{ required: true, trigger: "blur", validator: passwordValidator }],
  confirmPassword: [{ required: true, trigger: "blur", validator: confirmPasswordValidator }],
  desc: [{ required: true, trigger: "blur","message": "请输入描述"}],
  email: [{ required: true, trigger: "blur", validator: emailValidator }]
};

/**
 * 密码校验器
 */
function passwordValidator(rule: any, value: any, callback: any) {
  if (value.length < 6) {
    callback(new Error("密码少于六位"));
  } else {
    callback();
  }
}

/**
 * 确认密码校验器
 */
 function confirmPasswordValidator(rule: any, value: any, callback: any) {
  if (value.length <= 0) {
    callback(new Error("确认密码不能为空"));
  }else if(value != registerData.value.password){
    callback(new Error("两次密码输入不一致！"));
  } else {
    callback();
  }
}

/**
 * 邮箱校验器
 */
 function emailValidator(rule: any, value: any, callback: any) {
  var emailRegex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;
  if (!emailRegex.test(value)) {
    callback(new Error("请输入正确的邮箱格式"));
  } else {
    callback();
  }
}

/**
 * 检查输入大小写状态
 */
function checkCapslock(e: any) {
  const { key } = e;
  isCapslock.value = key && key.length === 1 && key >= "A" && key <= "Z";
}



/**
 * 登录
 */
function handleLogin() {
  registerFormRef.value.validate(async (valid: boolean) => {
    if (valid) {
      loading.value = true;
      const data = await registerTest(registerData.value);

      ElMessage({
        message: data.data.message,
        type: 'warning',
      })
      
      loading.value = false;
      
      data.data.message=="注册成功！"?router.push({ path: "/testlogin" }):"";

    }
  });
}


</script>

<style lang="scss" scoped>
.login-container {
  width: 100%;
  min-height: 100%;
  overflow: hidden;
  background-color: #2d3a4b;

  .login-form {
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;

    .captcha {
      position: absolute;
      top: 0;
      right: 0;

      img {
        width: 120px;
        height: 48px;
        cursor: pointer;
      }
    }
  }
}

.el-form-item {
  background: rgb(0 0 0 / 10%);
  border: 1px solid rgb(255 255 255 / 10%);
  border-radius: 5px;
}

.el-input {
  background: transparent;

  // 子组件 scoped 无效，使用 :deep
  :deep(.el-input__wrapper) {
    padding: 0;
    background: transparent;
    box-shadow: none;

    .el-input__inner {
      color: #fff;
      background: transparent;
      border: 0;
      border-radius: 0;
      caret-color: #fff;

      &:-webkit-autofill {
        box-shadow: 0 0 0 1000px transparent inset !important;
        -webkit-text-fill-color: #fff !important;
      }

      // 设置输入框自动填充的延迟属性
      &:-webkit-autofill,
      &:-webkit-autofill:hover,
      &:-webkit-autofill:focus,
      &:-webkit-autofill:active {
        transition: color 99999s ease-out, background-color 99999s ease-out;
        transition-delay: 99999s;
      }
    }
  }
}
</style>
